/**
 * Created by Amaris on 2016/12/6.
 */


var util = require('../utils/util.js');
var contactClass = require('../class/contact.js');

module.exports = {

    create: function (params, cb) {
        var models = util.models;
        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }

        var contact = new contactClass();

        var checkResult = contact.checkParams(
            [
                'seller',
                'name',
                'phone',
                'createUid'
            ],
            params);
        if (checkResult) {
            cb(util.getErrorRes('400', checkResult), null);
            return;
        } else {
            contact.init(params);
        }

        models.contact.create(contact.getParams(), function (err, data) {
            if (err || !data) {
                var err = new Error();
                err.status = 500;
                err.message = '系统错误';
                cb(err, null);
                return;
            }

            cb(null, util.getSuccessRes(data));
        });
    },

    getInfo: function (params, cb) {
        var models = util.models;

        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }

        var contact = new contactClass();

        var checkResult = contact.checkParams(['rowId'], params);
        if (checkResult) {
            cb(null, util.getErrorRes('400', checkResult));
            return;
        } else {
            contact.init(params);
        }

        models.contact.findOne(contact.getParams()).populate('seller').exec(function (err, data) {
            if (err || !data) {
                var err = new Error();
                err.status = 500;
                err.message = '系统错误';
                cb(err, null);
                return;
            }

            cb(null, util.getSuccessRes(data));

        });
    },
    //获取卖方联系人列表
    getContactListBySeller: function (params, cb) {
        var models = util.models;
        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }
        var contact = new contactClass();
        var checkResult = contact.checkParams(['seller'], params);
        if (checkResult) {
            cb(null, util.getErrorRes('400', checkResult));
            return;
        } else {
            contact.init(params);
        }

        models.contact.find(contact.getParams()).populate('seller').exec(function (err, data) {
            if (err || !data) {
                var err = new Error();
                err.status = 500;
                err.message = '系统错误';
                cb(err, null);
                return;
            }
            cb(null, util.getSuccessRes(data));
        });
    },
    //获取联系人列表
    getContactList: function (params, cb) {
        var models = util.models;
        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }

        models.contact.find().populate('seller').exec(function (err, data) {
            if (err || !data) {
                var err = new Error();
                err.status = 500;
                err.message = '系统错误';
                cb(err, null);
                return;
            }
            cb(null, util.getSuccessRes(data));
        });
    },
    //查询联系人
    QueryConatac: function (params, cb) {

        var models = util.models;
        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }

    },

    updateContact: function (params, cb) {

        var models = util.models;

        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = "数据库连接失败";
            cb(err, null);
            return;
        }

        var contact = new contactClass();
        var checkResult = contact.checkParams(
            ['rowId',
                'name'
            ], params);

        if (checkResult) {
            cb(util.getErrorRes('400', checkResult), null);
            return;
        } else {
            contact.init(params);
        }


        models.contact.update(params.rowId, contact.getParams()).exec(function (err, data) {
            if (err || !data) {
                console.log(err);
                var err = new Error();
                err.status = 500;
                err.message = '系统错误';
                cb(err, null);
                return;
            }
            if (data && data.length == 0) {
                var err = new Error();
                err.status = 401;
                err.message = '更新失败';
                cb(err, null);
                return;
            }
            cb(null, util.getSuccessRes(data));
        });
    },
    //todo 软删除
    //删除联系人
    delContact: function (params, cb) {
        var models = util.models;
        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }
        var rowId = params.rowId;
        models.contact.destroy({'rowId': rowId}).exec(function (err, data) {
            if (err || !data) {
                cb(err, null);
                return;
            }
            if (data && data.length == 0) {
                var err = new Error();
                err.status = 500;
                err.message = '删除失败';
                cb(err, null);
                return;
            }
            cb(null, util.getSuccessRes(data));
        });
    }

};



